<?php
/**
 * 易优CMS
 * ============================================================================
 * 版权所有 2016-2028 海南赞赞网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.eyoucms.com
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 * Author: 小虎哥 <1105415366@qq.com>
 * Date: 2018-4-3
 */

namespace think\template\taglib\api;

use think\Db;

/**
 * 优惠券领取列表
 */
class TagCouponList extends Base
{
    //初始化
    protected function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 获取优惠券领取列表
     * @author wengxianhu by 2018-4-20
     */
    public function getCouponList($users = [], $page = 1, $limit = 10)
    {
        $where['end_date'] = ['>', getTime()];
        $where['status'] = 1;
        $where['is_del'] = 0;


        $paginate = array(
            'page' => $page,
        );

        $pages = Db::name('shop_coupon')
            ->where($where)
            ->order('sort_order asc,coupon_id desc')
            ->paginate($limit, false, $paginate);
        $list = $pages->toArray();

        if (!empty($users['users_id']) && !empty($list['data'])) {
            $coupon_ids = get_arr_column($list['data'], 'coupon_id');
            //我领取的优惠券
            $my_coupon = Db::name('shop_coupon_use')->where('coupon_id', 'in', $coupon_ids)->where('use_status',0)->where('users_id', $users['users_id'])->group('coupon_id')->getAllWithIndex('coupon_id');
            if (!empty($my_coupon)) {
                foreach ($list['data'] as $k => $v) {
                    $list['data'][$k]['get'] = !empty($my_coupon[$v['coupon_id']]) ? $my_coupon[$v['coupon_id']] : '';
                }
            }
        }
        return $list;
    }

    //某商品可用优惠券
    public function getGoodCoupon($aid = 0, $users = [],$detail_get = 0)
    {
        $arcData = Db::name('archives')->where('aid',$aid)->find();
        if (empty($arcData)) return false;
        // 筛选条件
        $filter = [
            'status' => 1,
            'is_del' => 0
        ];
        if (0 < $detail_get) {
            $filter['detail_get'] = 1;//1-商品详情页可以直接领取
        }
        $filter['end_date'] = ['>=', getTime()];

        //先查出顶级栏目id
        $arctypeTopId = Db::name('arctype')->where('id', $arcData['typeid'])->value('topid');
        if (empty($arctypeTopId)) $arctypeTopId = $arcData['typeid'];

        $result = Db::name('shop_coupon')
            ->where($filter)
            ->where("coupon_type = 1 OR FIND_IN_SET('{$arctypeTopId}', arctype_id) OR FIND_IN_SET('{$arcData['aid']}', product_id)")
            ->order('sort_order asc,coupon_id desc')
            ->select();
        if (!empty($result)) {
            foreach ($result as $k => $v) {
                $v['coupon_form_name'] = '满减券';
                switch ($v['use_type']) {
                    case '1': // 固定期限有效
                        $v['start_date'] = date('Y/m/d', $v['start_date']);
                        $v['end_date'] = date('Y/m/d', $v['end_date']);
                        $v['use_type_name'] = $v['start_date'] . '-' . $v['end_date'];
                        break;
                    case '2'; // 领取当天开始有效
                        $v['use_type_name'] = '领取' . $v['valid_days'] . '天内有效';
                        break;
                    case '3'; // 领取次日开始有效
                        $v['use_type_name'] = '领取次日开始' . $v['valid_days'] . '天内有效';
                        break;
                    case '4'; // 长期有效
                        $v['use_type_name'] = '长期有效';
                        break;
                }
                switch ($v['coupon_type']) {
                    case '1':
                        $v['coupon_type_name'] = '全部商品';
                        break;
                    case '2';
                        $v['coupon_type_name'] = '指定商品';
                        break;
                    case '3';
                        $v['coupon_type_name'] = '指定分类';
                        break;
                }
                $result[$k] = $v;
            }
        }
        if (!empty($users['users_id']) && !empty($result)) {
            $coupon_ids = get_arr_column($result, 'coupon_id');
            //我领取的优惠券
            $my_coupon = Db::name('shop_coupon_use')->where('coupon_id', 'in', $coupon_ids)->where('use_status',0)->where('users_id', $users['users_id'])->group('coupon_id')->getAllWithIndex('coupon_id');
            if (!empty($my_coupon)) {
                foreach ($result as $k => $v) {
                    $result[$k]['get'] = !empty($my_coupon[$v['coupon_id']]) ? $my_coupon[$v['coupon_id']] : '';
                }
            }
        }
        return $result;
    }

    /**
     * 获取我的优惠券领取列表
     * @author wengxianhu by 2018-4-20
     */
    public function getMyCouponList($users = [], $page = 1, $limit = 10, $use_status = 0)
    {
        //判断是否有过期优惠券
        Db::name('shop_coupon_use')->where(['users_id' => $users['users_id'], 'use_status' => 0])->where('end_time', '<', getTime())->update(['use_status' => 2, 'update_time' => getTime()]);

        $where['a.users_id'] = $users['users_id'];
        $where['a.use_status'] = $use_status;
        $paginate = array(
            'page' => $page,
        );

        $pages = Db::name('shop_coupon_use')
            ->alias('a')
            ->field('b.*,a.*')
            ->join('shop_coupon b','a.coupon_id = b.coupon_id','left')
            ->where($where)
            ->order('end_time asc')
            ->paginate($limit, false, $paginate);
        $list = $pages->toArray();

        if (!empty($list['data'])){
            foreach ($list['data'] as $k => $v){
                $v['get_time'] = $this->time_format($v['get_time']);
                $v['use_time'] = $this->time_format($v['use_time']);
                $v['start_time'] = $this->time_format($v['start_time']);
                $v['end_time'] = $this->time_format($v['end_time']);
                $list['data'][$k] = $v;
            }
        }

        return $list;
    }
}